#include "TKEScreen.h"
#include "MAHeaders.h"

#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/ScrollPane.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/trim.h"

TKEScreen::TKEScreen(){
	mLayout = new Layout(0, 0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight(), NULL, 1, 3);

	//Create Main Title
	mTitle = new Label(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTBAR, mLayout);
	mTitle->setCaption("Luckyzone");
	mTitle->setHorizontalAlignment(Label::HA_CENTER);
	mTitle->setVerticalAlignment(Label::VA_CENTER);
	mTitle->setFont(APPCONTROLLER->getFont("default"));
	mTitle->setDrawBackground(true);
	mTitle->setPaddingLeft(0);
	mTitle->setPaddingRight(0);
	mTitle->setSkin(APPCONTROLLER->getSkin("title"));
	mTitle->update(); //Calculate the correct size

	//Create SoftKey Bar
	softKeys = createSoftKeyBar(HEIGHTBAR, " ", getStringNotIterative(L_BACK));

	//Create panel for content
	panel = new ScrollPane(0,0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight()-softKeys->getHeight()- mTitle->getHeight(), mLayout, 1, 1);
	panel->setDrawBackground(true);
	panel->setBackgroundColor(0);
	//panel->setSkin(APPCONTROLLER->getSkin("default"));
	mResult = new Label(0, 0, APPCONTROLLER->getScreenWidth(), 32, NULL, "",0, APPCONTROLLER->getFont("default"));
	mResult->setAutoSizeY(true);
	mResult->setMultiLine(true);
	panel->add(mResult);

	mLayout->add(softKeys);
	this->setMain(mLayout);
}

TKEScreen::~TKEScreen(){
	delete mLayout;
}

void TKEScreen::setTKEItem(LRESItem* item){
	mTitle->setCaption(item->companyname);
	mResult->setCaption(item->result);
	mTitle->update();
	mResult->update();
	panel->reset();
}

void TKEScreen::keyPressEvent(int keyCode, int nativeCode){
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			panel->scrollDown();
			break;
		case MAK_DOWN:
		case MAK_8:
			panel->scrollUp();
			break;
		case MAK_SOFTRIGHT:
			APPCONTROLLER->show("TKEIndex");
			break;
		case MAK_SOFTLEFT:
			break;
	}
}

void TKEScreen::pointerReleaseEvent(MAPoint2d point){

}

void TKEScreen::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (panel->contains(p)){

	}
	else if (softKeys->contains(p)){
		if(softKeys->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (softKeys->getChildren()[1]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

